package com.miracle9.game.dao;
import com.miracle9.game.entity.PayLog;
import com.miracle9.game.util.MyUtil;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class PayLogServiceDao extends SqlOperating<PayLog, Integer>
{
  public double getSumMoneyByType_0()
  {
    String hql = "select sum(money) from PayLog where type=0 and datetime>=? and parentPromoterId=0";
    Query query = q_CreateSql(hql, new Object[] { MyUtil.dateFormatYMD_Sting(new Date()) });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumMoneyByType_1()
  {
    String hql = "select sum(money) from PayLog where type=1 and datetime>=? and parentPromoterId=0";
    Query query = q_CreateSql(hql, new Object[] { MyUtil.dateFormatYMD_Sting(new Date()) });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumMoneyByUserId_0(int userId)
  {
    String hql = "select sum(money) from PayLog where type=1 and userId=? and datetime>=? and userType=0";
    Query query = q_CreateSql(hql, new Object[] { Integer.valueOf(userId), MyUtil.dateFormatYMD_Sting(new Date()) });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumMoneyByUserId_1(int promoterId)
  {
    String hql = "select sum(money) from PayLog where type=1 and userId=? and datetime>=? and userType=1";
    Query query = q_CreateSql(hql, new Object[] { Integer.valueOf(promoterId), MyUtil.dateFormatYMD_Sting(new Date()) });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public List<Object[]> getSumByDate(String date)
  {
    Query query = q_CreateSql("select type,sum(money),sum(gold) from PayLog where parentPromoterId = 0 and datetime like ? group by type", new Object[] {
      date + "%" });
    return query.list();
  }
  
  public void deleteByDate(String date)
  {
	  q_CreateSql("delete from PayLog where datetime like ?", new Object[] { date + "%" }).executeUpdate();
  }
  
  public int getSumGoldByInterval(String startDate, String endDate)
  {
    Object o = q_CreateSql("select sum(gold) from PayLog where datetime>=? and datetime<? and type=2", new Object[] { startDate, 
      endDate }).uniqueResult();
    if (o == null) {
      return 0;
    }
    return Integer.parseInt(o.toString());
  }
  
  public double getSumMoneyBy(int parentPromoterId, String date, int userType)
  {
    Query query;
    if ((userType == 0) || (userType == 1))
    {
      String hql = "select sum(money) from PayLog where type=0 and parentPromoterId=? and datetime like ? and userType=?";
      query = q_CreateSql(hql, new Object[] { Integer.valueOf(parentPromoterId), date + "%", Integer.valueOf(userType) });
    }
    else
    {
      String hql = "select sum(money) from PayLog where type=0 and parentPromoterId=? and datetime like ?";
      query = q_CreateSql(hql, new Object[] { Integer.valueOf(parentPromoterId), date + "%" });
    }
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumMoneyByUidAndDate_0(int userId, String date)
  {
    String hql = "select sum(money) from PayLog where type=0 and userId=? and datetime like ? and userType=0";
    Query query = q_CreateSql(hql, new Object[] { Integer.valueOf(userId), date + "%" });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumMoneyByUidAndDateType(int parentPromoterId, String date, int userType)
  {
    Query query;
    if ((userType == 0) || (userType == 1))
    {
      String hql = "select sum(money) from PayLog where type=1 and parentPromoterId=? and datetime like ? and userType=?";
      query = q_CreateSql(hql, new Object[] { Integer.valueOf(parentPromoterId), date + "%", Integer.valueOf(userType) });
    }
    else
    {
      String hql = "select sum(money) from PayLog where type=1 and parentPromoterId=? and datetime like ?";
      query = q_CreateSql(hql, new Object[] { Integer.valueOf(parentPromoterId), date + "%" });
    }
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumMoneyByUidAndDate_1(int userId, String date)
  {
    String hql = "select sum(money) from PayLog where type=1 and userId=? and datetime like ? and userType=0";
    Query query = q_CreateSql(hql, new Object[] { Integer.valueOf(userId), date + "%" });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public boolean saveReturnByType_0UserType_1(int userId, String userName, int gold, double money, String admin, int expiryType, String remark, int parentPromoterId, int promoterGold)
  {
    PayLog payLog = new PayLog(userId, userName, MyUtil.c(), money, gold, admin, 0, 0, 
      expiryType, remark, 1, parentPromoterId, promoterGold);
    if (((Integer)saveReturn(payLog)).intValue() > 0) {
      return true;
    }
    return false;
  }
  
  public boolean saveReturnByType_1UserType_1(int userId, String userName, int gold, double money, String admin, int expiryType, String remark, int parentPromoterId, int promoterGold)
  {
    PayLog payLog = new PayLog(userId, userName, MyUtil.c(), money, gold, admin, 1, 0, 
      expiryType, remark, 1, parentPromoterId, promoterGold);
    if (((Integer)saveReturn(payLog)).intValue() > 0) {
      return true;
    }
    return false;
  }
  
  public boolean saveReturnByType_0UserType_0(int userId, String userName, int gold, double money, String admin, int expiryType, String remark, int parentPromoterId, int promoterGold)
  {
    PayLog payLog = new PayLog(userId, userName, MyUtil.c(), money, gold, admin, 0, 0, 
      expiryType, remark, 0, parentPromoterId, promoterGold);
    if (((Integer)saveReturn(payLog)).intValue() > 0) {
      return true;
    }
    return false;
  }
  
  public boolean saveReturnByType_1UserType_0(int userId, String userName, int gold, double money, String admin, int expiryType, String remark, int parentPromoterId, int promoterGold)
  {
    PayLog payLog = new PayLog(userId, userName, MyUtil.c(), money, gold, admin, 1, 0, 
      expiryType, remark, 0, parentPromoterId, promoterGold);
    if (((Integer)saveReturn(payLog)).intValue() > 0) {
      return true;
    }
    return false;
  }
  
  public boolean saveReturnByTypeUserType_1(int childPromoterId, String childPromoterName, int minusGold, double money, String admin, int type, String remark, int parentPromoterId, int promoterGold)
  {
    PayLog payLog = new PayLog(childPromoterId, childPromoterName, MyUtil.c(), money, minusGold, admin, type, 0, 
      0, remark, 1, parentPromoterId, promoterGold);
    if (((Integer)saveReturn(payLog)).intValue() > 0) {
      return true;
    }
    return false;
  }
  
  public boolean saveReturnByTypeUserType_0(int userId, String userName, int gold, double money, String admin, int type, String remark, int parentPromoterId, int promoterGold)
  {
    PayLog payLog = new PayLog(userId, userName, MyUtil.c(), money, gold, admin, type, 0, 
      0, remark, 0, parentPromoterId, promoterGold);
    if (((Integer)saveReturn(payLog)).intValue() > 0) {
      return true;
    }
    return false;
  }
  
  public double getSumGoldByuserType_0(int userId, String date)
  {
    String hql = "select sum(gold) from PayLog where type=3 and userId=? and datetime like ? and userType=0";
    Query query = q_CreateSql(hql, new Object[] { Integer.valueOf(userId), date + "%" });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public double getSumGoldByuserType_1(int promoterId, String date)
  {
    String hql = "select sum(gold) from PayLog where type=3 and userId=? and datetime like ? and userType=1";
    Query query = q_CreateSql(hql, new Object[] { Integer.valueOf(promoterId), date + "%" });
    Object sum = query.uniqueResult();
    if (sum == null) {
      return 0.0D;
    }
    return Double.parseDouble(sum.toString());
  }
  
  public List<PayLog> getPayLogsByDay(String day)
  {
    return q_CreateSqlList("from PayLog where datetime like ?", new Object[] { day + "%" });
  }
}